//
//  WuView.swift
//  microBlog
//
//  Created by Wu YJ on 15/11/23.
//  Copyright © 2015年 Wu YJ. All rights reserved.
//

import UIKit

class WuView: UIView
{
    /**
    *  设置访客视图上面的元素, 在创建view的时候会调用initWithFrame:
    */
    
    override init(frame: CGRect)
    {
        //一般来说super的方法是设置在普通的存储类型变量的下面
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /**
    *  设置和准备UI界面的元素 , 元素（也就是界面的view使用懒加载来进行创建）
    */
    private func prepareUI()
    {
        addSubview(IconView)
        addSubview(panView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //关闭Autoreizing，不关闭可能会影响约束
        IconView.translatesAutoresizingMaskIntoConstraints = false
        panView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //转盘
        addConstraint(NSLayoutConstraint(item: panView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0))
        
        addConstraint(NSLayoutConstraint(item: panView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //房子
        addConstraint(NSLayoutConstraint(item: IconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: IconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        
        //消息
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: panView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

        
    }
    
    
    
    
    //懒加载UI元素
    
    
    
    
    //中间的图片
//    lazy var IconView : UIImageView =
//    {
//        let iconView = UIImageView()
//        iconView.image = UIImage(named: "visitordiscover_feed_image_house")
//        return iconView
//    }()
    
    lazy var IconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    
    
    
    
    //转盘
//    lazy var panView : UIImageView =
//    {
//        let pan = UIImageView()
//        pan.image = UIImage(named: "visitordiscover_feed_image_smallicon")
//        return pan
//    }()
    
    lazy var panView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    
    
    
    
    //图片下面的消息文本
    lazy var messageLabel : UILabel =
    {
        let msgView = UILabel()
        msgView.text = "关注一些人，看看有什么神奇"
        msgView.textColor = UIColor.init(red: 211/255.0, green: 211/255.0, blue: 211/255.0, alpha: 0.8)
        msgView.numberOfLines = 0
        //根据内容来适应大小
        msgView.sizeToFit()
        return msgView
    }()
    
    //注册按钮
    lazy var registerBtn : UIButton =
    {
        let regBtn = UIButton()
        //设置背景图片
        regBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置文本
        regBtn.setTitle("注册", forState: UIControlState.Normal)
        //设置文本的背景颜色
        regBtn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        //设置文本的大小
        regBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        //适应大小
        regBtn.sizeToFit()
        return regBtn
    }()
    
    //登录按钮
    lazy var loginBtn : UIButton =
    {
        let lgBtn = UIButton()
        //设置背景颜色
        lgBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置文本的背景颜色
        lgBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //设置文本
        lgBtn.setTitle("登录", forState: UIControlState.Normal)
        //设置文本大小
        lgBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        //适应大小
        lgBtn.sizeToFit()
        return lgBtn
    }()
    
}
